<script setup lang="ts">
import type { IProduct } from '@/api/interface/rescue/product/product'
import { computed, reactive, ref, watch } from 'vue'
import {
  createProductApi,
  getProductDetailApi,
  updateProductApi,
} from '@/api/modules/rescue/product/product'
import CategorySelect from '@/components/common/meta/CategorySelect.vue'
import TagSelect from '@/components/common/tag/TagSelect.vue'
import UploadImage from '@/components/common/upload/UploadImage.vue'
import WarehouseSelect from '@/components/rescue/WarehouseSelect.vue'

interface Props {
  /** 弹窗显示状态 */
  modelValue?: boolean
  /** 要编辑的物资数据 */
  product?: IProduct.Row
}

defineOptions({
  name: 'ProductForm',
})

const props = withDefaults(defineProps<Props>(), {
  modelValue: false,
  product: undefined,
})

const emit = defineEmits<{
  'update:modelValue': [value: boolean]
  success: []
  error: [message: string]
}>()

// 响应式数据
const visible = ref(props.modelValue)
const loading = ref(false)
const submitting = ref(false)
const isEdit = ref(false)
const productId = ref<number | undefined>(undefined)

// 监听外部状态变化
watch(
  () => props.modelValue,
  (newVal) => {
    visible.value = newVal
    if (newVal) {
      if (props.product) {
        // 编辑模式
        isEdit.value = true
        productId.value = props.product.id
        loadProductDetail(props.product.id!)
      } else {
        // 新增模式
        isEdit.value = false
        productId.value = undefined
        resetForm()
        addSku()
      }
    }
  },
  { immediate: true },
)

// 监听内部状态变化
watch(visible, (newVal) => {
  emit('update:modelValue', newVal)
})

// 表单数据
const formData = reactive<IProduct.ProductCreateDTO>({
  categoryId: undefined,
  warehouseId: undefined,
  name: '',
  image: '',
  productNumber: '',
  enable: true,
  tagIds: [],
  productSkus: [],
})

// 表单验证错误
const errors = ref<Record<string, string>>({})

// 计算属性
const hasSkus = computed(() => formData.productSkus.length > 0)

// 方法
async function open(options: { isEdit: boolean; productId?: number }) {
  visible.value = true
  isEdit.value = options.isEdit
  productId.value = options.productId

  // 重置表单
  resetForm()

  if (options.isEdit && options.productId) {
    await loadProductDetail(options.productId)
  } else {
    // 新增时添加一个默认规格
    addSku()
  }
}

function close() {
  visible.value = false
  resetForm()
  emit('update:modelValue', false)
}

function resetForm() {
  Object.assign(formData, {
    categoryId: undefined,
    warehouseId: undefined,
    name: '',
    image: '',
    productNumber: '',
    enable: true,
    tagIds: [],
    productSkus: [],
  })
  errors.value = {}
}

// 加载物资详情
async function loadProductDetail(id: number) {
  loading.value = true
  try {
    const response = await getProductDetailApi({ id })
    if (response.data) {
      const detail = response.data

      // 设置基本信息
      formData.categoryId = detail.categoryId
      formData.warehouseId = detail.warehouseId
      formData.name = detail.name || ''
      formData.image = detail.image || ''
      formData.productNumber = detail.productNumber || ''
      formData.enable = detail.enable || true

      // 处理标签
      formData.tagIds = detail.tags?.map((tag) => tag.id) || []

      // 处理物资规格
      if (detail.productSkus && detail.productSkus.length > 0) {
        formData.productSkus = detail.productSkus.map((sku) => ({
          id: sku.id,
          name: sku.name,
          image: sku.image,
          stock: sku.stock,
          price: sku.price,
          enable: sku.enable,
          productLots:
            sku.productLots?.map((lot) => ({
              id: lot.id,
              lotNumber: lot.lotNumber || '',
              title: lot.title || '',
              stock: lot.stock || 0,
              price: lot.price || 0,
              quantity: lot.quantity || 0,
              storageDate: lot.storageDate || '',
              productDate: lot.productDate || '',
              discardDate: lot.discardDate || '',
              overhaulLastDate: lot.overhaulLastDate || '',
              expirationTime: lot.expirationTime || '',
              overhaulGap: lot.overhaulGap || 0,
              overhaulNextDate: lot.overhaulNextDate || '',
            })) || [],
        }))
      }
    }
  } catch (error) {
    console.error('获取物资详情失败:', error)
    uni.showToast({
      title: '获取详情失败',
      icon: 'none',
    })
  } finally {
    loading.value = false
  }
}

// 规格操作
function addSku() {
  formData.productSkus.push({
    name: '',
    image: '',
    stock: 0,
    price: 0,
    enable: true,
    productLots: [],
  })
}

function removeSku(index: number) {
  if (formData.productSkus.length <= 1) {
    uni.showToast({
      title: '至少保留一个规格',
      icon: 'none',
    })
    return
  }
  formData.productSkus.splice(index, 1)
}

// 批次操作
function addLot(skuIndex: number) {
  if (!formData.productSkus[skuIndex].productLots) {
    formData.productSkus[skuIndex].productLots = []
  }
  formData.productSkus[skuIndex].productLots!.push({
    lotNumber: '',
    title: '',
    stock: 0,
    price: 0,
    quantity: 0,
    storageDate: '',
    productDate: '',
    discardDate: '',
    overhaulLastDate: '',
    expirationTime: '',
    overhaulGap: 0,
    overhaulNextDate: '',
  })
}

function removeLot(skuIndex: number, lotIndex: number) {
  if (formData.productSkus[skuIndex].productLots) {
    formData.productSkus[skuIndex].productLots!.splice(lotIndex, 1)
  }
}

// 填充测试数据
function fillTestData() {
  const currentDate = new Date().toISOString().split('T')[0]
  const nextYear = new Date()
  nextYear.setFullYear(nextYear.getFullYear() + 1)
  const nextYearDate = nextYear.toISOString().split('T')[0]

  Object.assign(formData, {
    categoryId: 1,
    warehouseId: 1,
    name: `测试物资-${Date.now()}`,
    image: `https://dummyimage.com/200x200/007AFF/fff&text=测试物资`,
    productNumber: `TEST-${Date.now()}`,
    enable: true,
    tagIds: [1, 2],
    productSkus: [
      {
        name: '标准规格',
        image: `https://dummyimage.com/150x150/34C759/fff&text=标准`,
        stock: 100,
        price: 299.99,
        enable: true,
        productLots: [
          {
            lotNumber: `LOT-${Date.now()}`,
            title: '2024年春季批次',
            stock: 50,
            price: 299.99,
            quantity: 50,
            storageDate: currentDate,
            productDate: currentDate,
            discardDate: nextYearDate,
            overhaulLastDate: currentDate,
            expirationTime: nextYearDate,
            overhaulGap: 30,
            overhaulNextDate: nextYearDate,
          },
        ],
      },
    ],
  })

  uni.showToast({
    title: '测试数据已填充',
    icon: 'success',
  })
}

// 表单验证
function validateForm() {
  errors.value = {}

  if (!formData.name?.trim()) {
    errors.value.name = '请输入物资名称'
  }

  if (!formData.categoryId) {
    errors.value.categoryId = '请选择分类'
  }

  if (!formData.warehouseId) {
    errors.value.warehouseId = '请选择仓库'
  }

  if (formData.productSkus.length === 0) {
    uni.showToast({
      title: '至少添加一个规格',
      icon: 'none',
    })
    return false
  }

  return Object.keys(errors.value).length === 0
}

// 复制物资编号
function copyProductNumber(productNumber: string) {
  uni.setClipboardData({
    data: productNumber,
    success: () => {
      uni.showToast({
        title: '编号已复制',
        icon: 'success',
        duration: 1500,
      })
    },
    fail: () => {
      uni.showToast({
        title: '复制失败',
        icon: 'none',
        duration: 1500,
      })
    },
  })
}

// 提交表单
async function handleSubmit() {
  if (!validateForm()) {
    return
  }

  submitting.value = true
  try {
    const submitData = { ...formData }

    // 如果是编辑模式，添加ID
    if (isEdit.value && productId.value) {
      submitData.id = productId.value
    }

    if (isEdit.value) {
      await updateProductApi(submitData)
      uni.showToast({
        title: '更新成功',
        icon: 'success',
      })
    } else {
      await createProductApi(submitData)
      uni.showToast({
        title: '创建成功',
        icon: 'success',
      })
    }

    emit('success')
    close()
  } catch (error) {
    console.error('提交失败:', error)
    const errorMessage = '提交失败'
    uni.showToast({
      title: errorMessage,
      icon: 'none',
    })
    emit('error', errorMessage)
  } finally {
    submitting.value = false
  }
}

// 暴露方法
defineExpose({
  open,
  close,
  copyProductNumber,
})
</script>

<template>
  <wd-popup
    v-model="visible"
    position="bottom"
    :close-on-click-modal="false"
    :safe-area-inset-bottom="true"
    :lock-scroll="true"
    class="product-form-popup"
  >
    <view class="form-container">
      <!-- 加载状态 -->
      <view v-if="loading" class="loading-overlay">
        <view class="loading-content">
          <wd-loading size="24" color="#007AFF" />
          <text class="loading-text">加载中...</text>
        </view>
      </view>
      <!-- 头部 -->
      <view class="form-header">
        <view class="header-content">
          <text class="header-title">
            {{ isEdit ? '编辑物资' : '新增物资' }}
          </text>
          <view class="header-actions">
            <wd-button type="text" size="small" class="test-btn" @click="fillTestData">
              测试数据
            </wd-button>
            <wd-icon name="close" size="20" class="close-btn" @click="close" />
          </view>
        </view>
      </view>

      <!-- 表单内容 -->
      <scroll-view class="form-content" scroll-y :show-scrollbar="false">
        <!-- 基本信息 -->
        <view class="form-section">
          <view class="section-header">
            <wd-icon name="info" size="16" color="#007AFF" />
            <text class="section-title">基本信息</text>
          </view>

          <view class="form-fields">
            <!-- 物资图片 -->
            <view class="field-group">
              <text class="field-label">物资图片</text>
              <UploadImage
                v-model:image-url="formData.image"
                :limit="1"
                width="120px"
                height="120px"
                class="image-upload"
              />
            </view>

            <!-- 物资名称 -->
            <view class="field-group">
              <text class="field-label required">物资名称</text>
              <wd-input
                v-model="formData.name"
                placeholder="请输入物资名称"
                clearable
                :error="!!errors.name"
                :error-message="errors.name"
                class="form-input"
              />
            </view>

            <!-- 物资编号 -->
            <view class="field-group">
              <text class="field-label">物资编号</text>
              <view class="product-number-wrapper">
                <wd-input
                  v-model="formData.productNumber"
                  placeholder="请输入物资编号"
                  clearable
                  class="form-input"
                />
                <wd-button
                  v-if="formData.productNumber"
                  type="text"
                  size="small"
                  class="copy-btn"
                  @click="copyProductNumber(formData.productNumber)"
                >
                  <wd-icon name="copy" size="14" />
                  <text>复制</text>
                </wd-button>
              </view>
            </view>

            <!-- 分类选择 -->
            <view class="field-group">
              <text class="field-label required">分类</text>
              <CategorySelect
                v-model="formData.categoryId"
                placeholder="请选择物资分类"
                :error="!!errors.categoryId"
                class="form-select"
              />
              <text v-if="errors.categoryId" class="error-text">
                {{ errors.categoryId }}
              </text>
            </view>

            <!-- 仓库选择 -->
            <view class="field-group">
              <text class="field-label required">仓库</text>
              <WarehouseSelect
                v-model="formData.warehouseId"
                placeholder="请选择物资仓库"
                :error="!!errors.warehouseId"
                class="form-select"
              />
              <text v-if="errors.warehouseId" class="error-text">
                {{ errors.warehouseId }}
              </text>
            </view>

            <!-- 标签选择 -->
            <view class="field-group">
              <text class="field-label">标签</text>
              <TagSelect
                v-model="formData.tagIds"
                :multiple="true"
                placeholder="请选择标签"
                class="form-select"
              />
            </view>

            <!-- 启用状态 -->
            <view class="field-group">
              <text class="field-label">启用状态</text>
              <wd-switch
                v-model="formData.enable"
                active-text="启用"
                inactive-text="禁用"
                class="form-switch"
              />
            </view>
          </view>
        </view>

        <!-- 规格信息 -->
        <view class="form-section">
          <view class="section-header">
            <wd-icon name="grid" size="16" color="#34C759" />
            <text class="section-title">规格信息</text>
            <wd-button type="primary" size="small" class="add-btn" @click="addSku">
              <wd-icon name="plus" size="14" />
              <text>添加规格</text>
            </wd-button>
          </view>

          <view class="sku-list">
            <view v-for="(sku, skuIndex) in formData.productSkus" :key="skuIndex" class="sku-card">
              <!-- 规格头部 -->
              <view class="sku-header">
                <text class="sku-title">规格 {{ skuIndex + 1 }}</text>
                <wd-button
                  type="error"
                  size="small"
                  class="remove-btn"
                  @click="removeSku(skuIndex)"
                >
                  <wd-icon name="delete" size="14" />
                </wd-button>
              </view>

              <!-- 规格信息 -->
              <view class="sku-fields">
                <!-- 规格图片 -->
                <view class="field-row">
                  <view class="field-group">
                    <text class="field-label">规格图片</text>
                    <UploadImage
                      v-model:image-url="sku.image"
                      :limit="1"
                      width="80px"
                      height="80px"
                      class="sku-image-upload"
                    />
                  </view>
                </view>

                <!-- 规格名称和库存 -->
                <view class="field-row">
                  <view class="field-group">
                    <text class="field-label">规格名称</text>
                    <wd-input
                      v-model="sku.name"
                      placeholder="请输入规格名称"
                      clearable
                      class="form-input"
                    />
                  </view>
                  <view class="field-group">
                    <text class="field-label">库存</text>
                    <wd-input-number
                      v-model="sku.stock"
                      :min="0"
                      placeholder="库存数量"
                      class="form-number"
                    />
                  </view>
                </view>

                <!-- 价格和状态 -->
                <view class="field-row">
                  <view class="field-group">
                    <text class="field-label">价格</text>
                    <wd-input-number
                      v-model="sku.price"
                      :min="0"
                      :precision="2"
                      placeholder="价格"
                      class="form-number"
                    />
                  </view>
                  <view class="field-group">
                    <text class="field-label">状态</text>
                    <wd-switch
                      v-model="sku.enable"
                      active-text="启用"
                      inactive-text="禁用"
                      class="sku-switch"
                    />
                  </view>
                </view>
              </view>

              <!-- 批次信息 -->
              <view class="lots-section">
                <view class="lots-header">
                  <text class="lots-title">批次信息</text>
                  <wd-button
                    type="primary"
                    size="small"
                    class="add-lot-btn"
                    @click="addLot(skuIndex)"
                  >
                    <wd-icon name="plus" size="12" />
                    <text>添加批次</text>
                  </wd-button>
                </view>

                <view class="lot-list">
                  <view v-for="(lot, lotIndex) in sku.productLots" :key="lotIndex" class="lot-card">
                    <!-- 批次头部 -->
                    <view class="lot-header">
                      <text class="lot-title">批次 {{ lotIndex + 1 }}</text>
                      <wd-button
                        type="error"
                        size="small"
                        class="remove-lot-btn"
                        @click="removeLot(skuIndex, lotIndex)"
                      >
                        <wd-icon name="delete" size="12" />
                      </wd-button>
                    </view>

                    <!-- 批次字段 -->
                    <view class="lot-fields">
                      <!-- 批次编号和名称 -->
                      <view class="field-row">
                        <view class="field-group">
                          <text class="field-label">批次编号</text>
                          <wd-input
                            v-model="lot.lotNumber"
                            placeholder="批次编号"
                            clearable
                            class="lot-input"
                          />
                        </view>
                        <view class="field-group">
                          <text class="field-label">批次名称</text>
                          <wd-input
                            v-model="lot.title"
                            placeholder="批次名称"
                            clearable
                            class="lot-input"
                          />
                        </view>
                      </view>

                      <!-- 库存和价格 -->
                      <view class="field-row">
                        <view class="field-group">
                          <text class="field-label">库存</text>
                          <wd-input-number
                            v-model="lot.stock"
                            :min="0"
                            placeholder="库存"
                            class="lot-number"
                          />
                        </view>
                        <view class="field-group">
                          <text class="field-label">价格</text>
                          <wd-input-number
                            v-model="lot.price"
                            :min="0"
                            :precision="2"
                            placeholder="价格"
                            class="lot-number"
                          />
                        </view>
                      </view>

                      <!-- 入库数量和检修间隔 -->
                      <view class="field-row">
                        <view class="field-group">
                          <text class="field-label">入库数量</text>
                          <wd-input-number
                            v-model="lot.quantity"
                            :min="0"
                            placeholder="入库数量"
                            class="lot-number"
                          />
                        </view>
                        <view class="field-group">
                          <text class="field-label">检修间隔(天)</text>
                          <wd-input-number
                            v-model="lot.overhaulGap"
                            :min="0"
                            placeholder="检修间隔"
                            class="lot-number"
                          />
                        </view>
                      </view>

                      <!-- 日期信息 -->
                      <view class="date-section">
                        <view class="field-row">
                          <view class="field-group">
                            <text class="field-label">生产日期</text>
                            <wd-datetime-picker
                              v-model="lot.productDate"
                              placeholder="生产日期"
                              type="date"
                              class="date-picker"
                            />
                          </view>
                          <view class="field-group">
                            <text class="field-label">入库时间</text>
                            <wd-datetime-picker
                              v-model="lot.storageDate"
                              placeholder="入库时间"
                              type="date"
                              class="date-picker"
                            />
                          </view>
                        </view>

                        <view class="field-row">
                          <view class="field-group">
                            <text class="field-label">过期时间</text>
                            <wd-datetime-picker
                              v-model="lot.expirationTime"
                              placeholder="过期时间"
                              type="date"
                              class="date-picker"
                            />
                          </view>
                          <view class="field-group">
                            <text class="field-label">报废日期</text>
                            <wd-datetime-picker
                              v-model="lot.discardDate"
                              placeholder="报废日期"
                              type="date"
                              class="date-picker"
                            />
                          </view>
                        </view>

                        <view class="field-row">
                          <view class="field-group">
                            <text class="field-label">上次检修</text>
                            <wd-datetime-picker
                              v-model="lot.overhaulLastDate"
                              placeholder="上次检修"
                              type="date"
                              class="date-picker"
                            />
                          </view>
                          <view class="field-group">
                            <text class="field-label">下次检修</text>
                            <wd-datetime-picker
                              v-model="lot.overhaulNextDate"
                              placeholder="下次检修"
                              type="date"
                              class="date-picker"
                            />
                          </view>
                        </view>
                      </view>
                    </view>
                  </view>
                </view>
              </view>
            </view>
          </view>
        </view>
      </scroll-view>

      <!-- 底部操作按钮 -->
      <view class="form-footer">
        <wd-button type="text" class="footer-btn cancel-btn" @click="close">取消</wd-button>
        <wd-button
          type="primary"
          :loading="submitting"
          class="footer-btn submit-btn"
          @click="handleSubmit"
        >
          {{ isEdit ? '更新' : '创建' }}
        </wd-button>
      </view>
    </view>
  </wd-popup>
</template>

<style lang="scss" scoped>
// Apple 设计系统变量
$system-blue: #007aff;
$system-green: #34c759;
$system-orange: #ff9500;
$system-red: #ff3b30;
$system-gray: #8e8e93;
$label-primary: #000000;
$label-secondary: rgba(60, 60, 67, 0.6);
$background-primary: #ffffff;
$background-secondary: #f2f2f7;
$separator: rgba(60, 60, 67, 0.36);

.product-form-popup {
  // 确保弹窗在最上层，避免被其他元素遮挡
  :deep(.wd-popup) {
    z-index: 10000;
  }

  // 全局移动端优化
  .form-container {
    box-sizing: border-box;
  }

  // 防止任何元素超出容器宽度
  :deep(.form-container) {
    max-width: 100%;
  }

  .form-container {
    background-color: $background-primary;
    border-radius: 20px 20px 0 0; // Apple风格的圆角
    height: 92vh; // 增加高度，提供更多空间
    display: flex;
    flex-direction: column;
    position: relative;
    width: 100%;
    max-width: 100vw; // 确保不超过视口宽度
    overflow: hidden; // 防止内容溢出
    box-sizing: border-box;
    box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.1); // Apple风格的阴影

    .loading-overlay {
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background-color: rgba(255, 255, 255, 0.95);
      display: flex;
      align-items: center;
      justify-content: center;
      z-index: 1000;
      border-radius: 20px 20px 0 0;
      backdrop-filter: blur(4px);

      .loading-content {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 16px;
        background: $background-primary;
        padding: 24px 32px;
        border-radius: 16px;
        box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);

        .loading-text {
          font-size: 15px;
          color: $system-gray;
          font-weight: 600;
          letter-spacing: -0.2px;
        }
      }
    }

    .form-header {
      padding: 16px 20px;
      border-bottom: 1px solid rgba(0, 0, 0, 0.06);
      background: $background-primary;
      flex-shrink: 0; // 防止头部被压缩
      position: relative;

      // Apple风格的顶部指示器
      &::before {
        content: '';
        position: absolute;
        top: 8px;
        left: 50%;
        transform: translateX(-50%);
        width: 36px;
        height: 4px;
        background-color: rgba(0, 0, 0, 0.2);
        border-radius: 2px;
      }

      .header-content {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-top: 8px;

        .header-title {
          font-size: 20px;
          font-weight: 700; // Apple风格的粗体
          color: $label-primary;
          letter-spacing: -0.5px; // Apple风格的字母间距
        }

        .header-actions {
          display: flex;
          align-items: center;
          gap: 12px;

          .test-btn {
            padding: 8px 12px;
            background-color: rgba(0, 122, 255, 0.08);
            border-radius: 12px;
            color: $system-blue;
            font-size: 13px;
            font-weight: 600;
            transition: all 0.2s ease;
            border: 1px solid rgba(0, 122, 255, 0.15);

            &:active {
              background-color: rgba(0, 122, 255, 0.15);
              transform: scale(0.96);
            }
          }

          .close-btn {
            padding: 8px;
            color: $system-gray;
            transition: all 0.2s ease;
            border-radius: 10px;

            &:active {
              color: $label-primary;
              background-color: rgba(0, 0, 0, 0.05);
              transform: scale(0.9);
            }
          }
        }
      }
    }

    .form-content {
      flex: 1;
      padding: 20px;
      overflow-y: auto;
      overflow-x: hidden; // 防止水平滚动
      -webkit-overflow-scrolling: touch; // iOS 平滑滚动
      width: 100%;
      box-sizing: border-box;
      background: linear-gradient(180deg, $background-primary 0%, rgba(242, 242, 247, 0.3) 100%);

      .form-section {
        margin-bottom: 24px;
        background: $background-primary;
        border-radius: 16px;
        padding: 20px;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
        border: 1px solid rgba(0, 0, 0, 0.04);

        .section-header {
          display: flex;
          align-items: center;
          justify-content: space-between;
          margin-bottom: 16px;
          padding-bottom: 12px;
          border-bottom: 1px solid rgba(0, 0, 0, 0.06);

          .section-title {
            display: flex;
            align-items: center;
            gap: 10px;
            font-size: 18px;
            font-weight: 700;
            color: $label-primary;
            letter-spacing: -0.3px;
          }

          .add-btn {
            display: flex;
            align-items: center;
            gap: 6px;
            padding: 8px 16px;
            border-radius: 16px;
            background: linear-gradient(135deg, $system-blue 0%, darken($system-blue, 5%) 100%);
            color: white;
            font-size: 14px;
            font-weight: 600;
            transition: all 0.2s ease;
            box-shadow: 0 2px 8px rgba(0, 122, 255, 0.2);

            &:active {
              background: linear-gradient(
                135deg,
                darken($system-blue, 10%) 0%,
                darken($system-blue, 15%) 100%
              );
              transform: scale(0.96);
              box-shadow: 0 1px 4px rgba(0, 122, 255, 0.3);
            }
          }
        }

        .form-fields {
          .field-group {
            margin-bottom: 18px;

            .field-label {
              display: block;
              font-size: 15px;
              font-weight: 600;
              color: $label-primary;
              margin-bottom: 8px;
              letter-spacing: -0.2px;

              &.required::after {
                content: ' *';
                color: $system-red;
                font-weight: 700;
              }
            }

            .product-number-wrapper {
              display: flex;
              gap: 10px;
              align-items: center;

              .form-input {
                flex: 1;
              }

              .copy-btn {
                flex-shrink: 0;
                padding: 8px 14px;
                border-radius: 12px;
                background: linear-gradient(
                  135deg,
                  rgba(0, 122, 255, 0.08) 0%,
                  rgba(0, 122, 255, 0.12) 100%
                );
                color: $system-blue;
                font-size: 13px;
                font-weight: 600;
                transition: all 0.2s ease;
                border: 1px solid rgba(0, 122, 255, 0.15);
                box-shadow: 0 1px 3px rgba(0, 122, 255, 0.1);

                &:active {
                  background: linear-gradient(
                    135deg,
                    rgba(0, 122, 255, 0.15) 0%,
                    rgba(0, 122, 255, 0.2) 100%
                  );
                  transform: scale(0.96);
                }
              }
            }

            .form-input,
            .form-select,
            .form-number {
              width: 100%;
              border-radius: 14px;
              border: 1px solid rgba(0, 0, 0, 0.08);
              background-color: $background-primary;
              padding: 14px 16px;
              font-size: 16px;
              font-weight: 500;
              transition: all 0.2s ease;
              box-sizing: border-box;
              min-width: 0; // 防止flex子项收缩问题
              box-shadow: 0 1px 3px rgba(0, 0, 0, 0.04);

              &:focus {
                border-color: $system-blue;
                box-shadow:
                  0 0 0 3px rgba(0, 122, 255, 0.1),
                  0 2px 8px rgba(0, 122, 255, 0.1);
                outline: none;
              }

              &::placeholder {
                color: rgba(60, 60, 67, 0.3);
                font-weight: 400;
              }
            }

            .form-switch {
              margin-top: 6px;
            }

            .image-upload {
              display: flex;
              justify-content: center;
            }

            .error-text {
              font-size: 13px;
              color: $system-red;
              margin-top: 6px;
              font-weight: 500;
            }
          }
        }

        .sku-list {
          .sku-card {
            background: linear-gradient(
              135deg,
              $background-secondary 0%,
              rgba(255, 255, 255, 0.8) 100%
            );
            border-radius: 16px;
            padding: 18px;
            margin-bottom: 16px;
            border: 1px solid rgba(0, 0, 0, 0.06);
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
            transition: all 0.2s ease;

            &:hover {
              box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
              transform: translateY(-1px);
            }

            .sku-header {
              display: flex;
              justify-content: space-between;
              align-items: center;
              margin-bottom: 16px;
              padding-bottom: 12px;
              border-bottom: 1px solid rgba(0, 0, 0, 0.06);

              .sku-title {
                font-size: 17px;
                font-weight: 700;
                color: $label-primary;
                letter-spacing: -0.3px;
              }

              .remove-btn {
                display: flex;
                align-items: center;
                justify-content: center;
                width: 32px;
                height: 32px;
                border-radius: 16px;
                background: linear-gradient(
                  135deg,
                  rgba(255, 59, 48, 0.1) 0%,
                  rgba(255, 59, 48, 0.15) 100%
                );
                color: $system-red;
                transition: all 0.2s ease;
                border: 1px solid rgba(255, 59, 48, 0.2);

                &:active {
                  background: linear-gradient(
                    135deg,
                    rgba(255, 59, 48, 0.2) 0%,
                    rgba(255, 59, 48, 0.25) 100%
                  );
                  transform: scale(0.9);
                }
              }
            }

            .sku-fields {
              .field-row {
                display: block; // 移动端优先使用垂直布局
                margin-bottom: 10px;

                @media (min-width: 768px) {
                  display: flex;
                  gap: 10px;
                }

                .field-group {
                  width: 100%;
                  margin-bottom: 10px;

                  @media (min-width: 768px) {
                    flex: 1;
                    margin-bottom: 0;

                    &:first-child {
                      flex: 2;
                    }
                  }

                  .field-label {
                    font-size: 13px;
                    margin-bottom: 4px;
                    color: $label-secondary;
                  }

                  .sku-image-upload {
                    display: flex;
                    justify-content: flex-start;
                  }

                  .sku-switch {
                    margin-top: 2px;
                  }

                  .form-input,
                  .form-number {
                    width: 100%;
                    padding: 8px 10px;
                    font-size: 14px;
                    border-radius: 8px;
                    box-sizing: border-box;
                  }
                }
              }
            }

            .lots-section {
              margin-top: 16px;
              padding-top: 16px;
              border-top: 1px solid rgba(0, 0, 0, 0.08);

              .lots-header {
                display: flex;
                justify-content: space-between;
                align-items: center;
                margin-bottom: 12px;

                .lots-title {
                  font-size: 15px;
                  font-weight: 700;
                  color: $label-primary;
                  letter-spacing: -0.2px;
                }

                .add-lot-btn {
                  display: flex;
                  align-items: center;
                  gap: 4px;
                  padding: 6px 12px;
                  border-radius: 12px;
                  background: linear-gradient(
                    135deg,
                    rgba(0, 122, 255, 0.08) 0%,
                    rgba(0, 122, 255, 0.12) 100%
                  );
                  color: $system-blue;
                  font-size: 12px;
                  font-weight: 600;
                  transition: all 0.2s ease;
                  border: 1px solid rgba(0, 122, 255, 0.15);
                  box-shadow: 0 1px 3px rgba(0, 122, 255, 0.1);

                  &:active {
                    background: linear-gradient(
                      135deg,
                      rgba(0, 122, 255, 0.15) 0%,
                      rgba(0, 122, 255, 0.2) 100%
                    );
                    transform: scale(0.96);
                  }
                }
              }

              .lot-list {
                .lot-card {
                  background: linear-gradient(
                    135deg,
                    $background-primary 0%,
                    rgba(248, 248, 250, 0.8) 100%
                  );
                  border-radius: 12px;
                  padding: 14px;
                  margin-bottom: 8px;
                  border: 1px solid rgba(0, 0, 0, 0.06);
                  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.03);
                  transition: all 0.2s ease;

                  &:hover {
                    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.06);
                    transform: translateY(-1px);
                  }

                  .lot-header {
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                    margin-bottom: 12px;
                    padding-bottom: 8px;
                    border-bottom: 1px solid rgba(0, 0, 0, 0.05);

                    .lot-title {
                      font-size: 14px;
                      font-weight: 700;
                      color: $label-primary;
                      letter-spacing: -0.2px;
                    }

                    .remove-lot-btn {
                      display: flex;
                      align-items: center;
                      justify-content: center;
                      width: 26px;
                      height: 26px;
                      border-radius: 13px;
                      background: linear-gradient(
                        135deg,
                        rgba(255, 59, 48, 0.1) 0%,
                        rgba(255, 59, 48, 0.15) 100%
                      );
                      color: $system-red;
                      transition: all 0.2s ease;
                      border: 1px solid rgba(255, 59, 48, 0.2);

                      &:active {
                        background: linear-gradient(
                          135deg,
                          rgba(255, 59, 48, 0.2) 0%,
                          rgba(255, 59, 48, 0.25) 100%
                        );
                        transform: scale(0.9);
                      }
                    }
                  }

                  .lot-fields {
                    .field-row {
                      display: block; // 移动端优先垂直布局
                      margin-bottom: 8px;

                      @media (min-width: 768px) {
                        display: flex;
                        gap: 6px;
                        margin-bottom: 6px;
                      }

                      .field-group {
                        width: 100%;
                        margin-bottom: 8px;

                        @media (min-width: 768px) {
                          flex: 1;
                          margin-bottom: 0;
                        }

                        .field-label {
                          font-size: 11px;
                          margin-bottom: 3px;
                          color: $label-secondary;
                          font-weight: 500;
                        }

                        .lot-input,
                        .lot-number,
                        .date-picker {
                          width: 100%;
                          border-radius: 10px;
                          border: 1px solid rgba(0, 0, 0, 0.08);
                          font-size: 13px;
                          padding: 8px 12px;
                          background-color: #fff;
                          transition: all 0.2s ease;
                          box-sizing: border-box;
                          box-shadow: 0 1px 2px rgba(0, 0, 0, 0.02);

                          &:focus {
                            border-color: $system-blue;
                            box-shadow:
                              0 0 0 3px rgba(0, 122, 255, 0.1),
                              0 2px 6px rgba(0, 122, 255, 0.1);
                            outline: none;
                          }

                          &::placeholder {
                            color: rgba(60, 60, 67, 0.3);
                            font-weight: 400;
                          }
                        }
                      }
                    }

                    .date-section {
                      margin-top: 8px;
                      padding-top: 8px;
                      border-top: 1px solid rgba(0, 0, 0, 0.06);
                    }
                  }
                }
              }
            }
          }
        }
      }
    }

    .form-footer {
      display: flex;
      gap: 16px;
      padding: 16px 20px;
      border-top: 1px solid rgba(0, 0, 0, 0.06);
      background: $background-primary;
      flex-shrink: 0; // 防止底部被压缩
      box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);

      .footer-btn {
        flex: 1;
        padding: 16px 0;
        border-radius: 16px;
        font-weight: 700;
        font-size: 17px;
        transition: all 0.2s ease;
        letter-spacing: -0.3px;

        &.cancel-btn {
          background: linear-gradient(
            135deg,
            $background-secondary 0%,
            darken($background-secondary, 3%) 100%
          );
          color: $label-primary;
          border: 1px solid rgba(0, 0, 0, 0.08);
          box-shadow: 0 2px 6px rgba(0, 0, 0, 0.04);

          &:active {
            background: linear-gradient(
              135deg,
              darken($background-secondary, 5%) 0%,
              darken($background-secondary, 8%) 100%
            );
            transform: scale(0.96);
          }
        }

        &.submit-btn {
          background: linear-gradient(135deg, $system-blue 0%, darken($system-blue, 5%) 100%);
          color: white;
          border: none;
          box-shadow: 0 4px 12px rgba(0, 122, 255, 0.3);

          &:active {
            background: linear-gradient(
              135deg,
              darken($system-blue, 10%) 0%,
              darken($system-blue, 15%) 100%
            );
            transform: scale(0.96);
            box-shadow: 0 2px 8px rgba(0, 122, 255, 0.4);
          }
        }
      }
    }
  }
}

// Apple风格的动画效果
.form-container {
  animation: slideUp 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes slideUp {
  from {
    transform: translateY(100%);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

.form-section {
  animation: fadeInUp 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  animation-fill-mode: both;

  &:nth-child(1) {
    animation-delay: 0.1s;
  }
  &:nth-child(2) {
    animation-delay: 0.2s;
  }
}

@keyframes fadeInUp {
  from {
    transform: translateY(20px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

// 响应式设计
@media (max-width: 375px) {
  .form-container {
    height: 95vh; // 小屏幕增加高度

    .form-header {
      padding: 10px 12px;

      .header-content {
        .header-title {
          font-size: 16px;
        }

        .header-actions {
          gap: 6px;

          .test-btn {
            padding: 3px 6px;
            font-size: 11px;
          }
        }
      }
    }

    .form-content {
      padding: 12px;

      .form-section {
        margin-bottom: 16px;

        .section-header {
          .section-title {
            font-size: 15px;
          }

          .add-btn {
            padding: 4px 8px;
            font-size: 11px;
          }
        }

        .form-fields {
          .field-group {
            margin-bottom: 12px;

            .field-label {
              font-size: 13px;
            }

            .form-input,
            .form-select,
            .form-number {
              padding: 10px;
              font-size: 15px;
              width: 100% !important;
            }
          }
        }

        .sku-list .sku-card {
          padding: 10px;

          .sku-header .sku-title {
            font-size: 15px;
          }
        }
      }

      .field-row {
        display: block !important; // 强制垂直布局
        gap: 6px;

        .field-group {
          width: 100% !important;
          margin-bottom: 8px !important;
        }
      }
    }

    .form-footer {
      padding: 10px 12px;

      .footer-btn {
        padding: 12px 0;
        font-size: 15px;
      }
    }
  }
}

// 极小屏幕优化（如iPhone SE）
@media (max-width: 320px) {
  .form-container {
    .form-content {
      padding: 8px;

      .form-section {
        margin-bottom: 12px;

        .sku-list .sku-card {
          padding: 8px;

          .lots-section .lot-list .lot-card {
            padding: 6px;

            .lot-fields {
              .lot-input,
              .lot-number,
              .date-picker {
                padding: 4px 6px;
                font-size: 12px;
              }
            }
          }
        }
      }
    }
  }
}

// 大屏幕优化
@media (min-width: 768px) {
  .form-container {
    height: 85vh;

    .form-content {
      padding: 20px 24px;

      .form-section {
        .section-header {
          .section-title {
            font-size: 17px;
          }
        }

        .sku-list .sku-card {
          padding: 16px;
        }
      }
    }

    .form-footer {
      padding: 16px 24px;
    }
  }
}
</style>
